import pandas as pd
import numpy as np
def getTargetValue(full_grade_list,regret_grades,target_count=4):
    '''

    :param full_grade_list: c写的那个文件的返回（list）,只要后几列（目标分值）
    :param regret_grades: 规范化后的成绩
    :param target_count:要计算的课程目标个数
    :return: list，各目标分值
    '''

    link_counts = len(full_grade_list)
    topic_list=[]
    for i in range(link_counts):
        for j in range(3,len(full_grade_list[i]),4):
            for item in full_grade_list[i][j][3:]:
                topic_list.append(item[3:])
    for i in topic_list:
        for j in range(len(i)):
            try:
                i[j]=float(i[j])
            except:
                i[j]=0
    topic_list=pd.DataFrame(topic_list)
    target_por=[]
    for i in range(target_count):
        target_por.append(topic_list.iloc[:,i].tolist())

    # target_por=np.array(target_por)
    target_por=pd.DataFrame(target_por)
    length=len(regret_grades[0][0])
    target_por1=np.array(target_por.iloc[:,:length])
    target_por2 = np.array(target_por.iloc[:, length:])
    target_por=[target_por1,target_por2]
    target_value_list=[]
    for i in range(len(regret_grades)):
        temp_list = []
        for j in range(len(regret_grades[i])):
            grades=regret_grades[i][j]
            temp=target_por[i]*np.array(grades)
            temp_list.append(list(np.sum(temp, axis = 1)))
            # target_value_list.append(list(np.sum(temp,axis=1)))
        target_value_list.append(temp_list)
    return target_value_list


# import json
# path='tree.json'
# f = open(path)
# m = json.load(f)
# regret_grades=[[[48.0,24.0],[48.0,24.0]],[[48.0,24.0,14.75],[48.0,24.0,14.75]]]
# print(getTargetValue(m, regret_grades))